const HEX: &str = "0123456789abcdef";

pub fn convert_base(num_str: &str, to_base: u32) -> String {
    let parts: Vec<&str> = num_str.split(&['(', ')'][..]).collect();
    let decimalism = to_decimalism(parts[0], parts[1].parse::<u32>().unwrap());
    let sum = decimalism_to_n(decimalism, to_base);
    // sum
    sum.to_string()
}

fn to_decimalism(ss: &str, n: u32) -> u32 {
  let mut sum: u32 = 0;
  let mut i = ss.len() as u32;
  for char in ss.chars() {
    i -= 1;
    let t = HEX.find(char).unwrap() as u32;
    sum += t * n.pow(i);
  }
  sum
}

fn decimalism_to_n(sum: u32, n: u32) -> String {
  let mut i = sum;
  let mut res = String::new();
  while i > 0 {
    let t = i % n;
    res = HEX.chars().nth(t as usize).unwrap().to_string() + &res;
    i /= n;
  }
  res
}
